<?php

// @formatter:off
/**
 * @file WCFVTopBar.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCFVTopBar.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/includes/WCFVTopBar.php $
 */
// @formatter:on

class WCFVTopBarNode {
	//
	// Protected properties.
	protected $_cssClass = false;
	protected $_ficon = false;
	protected $_icon = false;
	protected $_title = false;
	protected $_url = false;
	//
	// Magic methods.
	public function __construct($title) {
		$this->setTitle($title);
	}
	//
	// Public methods.
	public function cssClass() {
		return $this->_cssClass;
	}
	public function ficon() {
		return $this->_ficon;
	}
	public function icon() {
		return $this->_icon;
	}
	public function setCssClass($cssClass) {
		$this->_cssClass = $cssClass;
		return $this->cssClass();
	}
	public function setFicon($ficon) {
		$this->_ficon = $ficon;
		return $this->ficon();
	}
	public function setFromArray(array $list) {
		if(isset($list["class"])) {
			$this->setCssClass($list["class"]);
		}
		if(isset($list["ficon"])) {
			$this->setFicon($list["ficon"]);
		}
		if(isset($list["icon"])) {
			$this->setIcon($list["icon"]);
		}
		if(isset($list["title"])) {
			$this->setTitle($list["title"]);
		}
		if(isset($list["url"])) {
			$this->setUrl($list["url"]);
		}

		return $this->toItem();
	}
	public function setIcon($icon) {
		$this->_icon = $icon;
		return $this->icon();
	}
	public function setTitle($title) {
		$this->_title = $title;
		return $this->title();
	}
	public function setUrl($url) {
		$this->_url = $url;
		return $this->url();
	}
	public function toItem() {
		$out = array();

		$out["class"] = $this->cssClass();
		$out["ficon"] = $this->ficon();
		$out["icon"] = $this->icon();
		$out["title"] = $this->title();
		$out["url"] = $this->url();

		return array_filter($out);
	}
	public function title() {
		return $this->_title;
	}
	public function url() {
		return $this->_url;
	}
}

class WCFVTopBar extends WCFactory {
	const LeftSide = "left";
	const RightSide = "right";
	//
	// Public class methods.
	public static function GetTopBarItems($side, $toItem = true) {
		$items = array();
//
//		self::TriggerTopBarBuilders($side);

		switch($side) {
			case self::LeftSide:
				$items = self::GetLeftSideItems();
				break;
			case self::RightSide:
				$items = self::GetRightSideItems();
				break;
			default:
				// @todo extra menu
				break;
		}

		if($toItem) {
			$aux = array();
			foreach($items as $item) {
				$aux[] = $item->toItem();
			}
			$items = $aux;
		}

		return $items;
	}
	//
	// Protected class methods.
	protected static function GetLeftSideItems() {
		$items = array();
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcGroup;
		global $wcUser;
		global $wcDefaults;
//		global $wcMenuItems;

		$lang = WCTranslate::I();
		$groupId = $wcGroup->id;

//		foreach($wcMenuItems[WC_MENU_CODE_MAIN]["top"] as $item) {
//			$aux = new WCFVTopBarNode("unnamed");
//			$aux->setFromArray($item);
//			$items[] = $aux;
//		}

		$aux = new WCFVTopBarNode($lang->translate("menu_label_home"));
		$aux->setIcon("wcomix.png");
		$aux->setCssClass("wc-topbar-home");
		$items[] = $aux;

		$aux = new WCFVTopBarNode($lang->translate("menu_label_main"));
		$aux->setIcon("mainmenu-icon.png");
		$aux->setCssClass("wc-topbar-menu-main");
		$items[] = $aux;

		if($wcUser && $wcUser->ok() && WCProfile::HasProfile($wcUser, "ADMIN", $wcGroup)) {
			$aux = new WCFVTopBarNode($lang->translate("menu_label_admin"));
			$aux->setIcon("admin-icon.png");
			$aux->setCssClass("wc-topbar-menu-admin");
			$items[] = $aux;
		}

		$aux = new WCFVTopBarNode($lang->translate("menu_label_shortcuts"));
		$aux->setIcon("tags-icon.png");
		$aux->setCssClass("wc-topbar-menu-shortcuts");
		$items[] = $aux;

		return $items;
	}
	protected static function GetRightSideItems() {
		$items = array();
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcGroup;
		global $wcMenuItems;
		global $wcUser;

		$lang = WCTranslate::I();
		$groupId = $wcGroup->id;

		$aux = new WCFVTopBarNode($lang->translate("menu_label_user"));
		$aux->setCssClass("wc-topbar-menu-user");
		if($wcUser && $wcUser->ok()) {
			if($wcUser->id() == WC_USER_ROOT_ID) {
				$aux->setFicon(WCViewManager::I()->SITE_URI."/images/avatars/root.png");
			} else {
				$aux->setIcon("user-icon.png");
			}
		} else {
			$aux->setIcon("no-user-icon.png");
		}
		$items[] = $aux;

		return $items;
	}
	protected static function TriggerTopBarBuilders($side) {
//		global $wcMenuBuilders;
//
//		foreach($wcMenuBuilders as $builder) {
//			$builder($side);
//		}
	}
}
